package com.foodorder.controller;

import com.foodorder.dto.ApiResponse;
import com.foodorder.dto.LoginRequest;
import com.foodorder.dto.LoginResponse;
import com.foodorder.service.AdminService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class AdminController {
    
    private final AdminService adminService;
    
    /**
     * 管理员登录
     */
    @PostMapping("/login")
    public ApiResponse<LoginResponse> adminLogin(@RequestBody LoginRequest request) {
        LoginResponse response = adminService.adminLogin(request);
        return ApiResponse.success(response);
    }
    
    /**
     * 测试数据库连接和管理员数据
     */
    @GetMapping("/test-db")
    public ApiResponse<Map<String, Object>> testDatabase() {
        Map<String, Object> result = adminService.testDatabase();
        return ApiResponse.success(result);
    }
    
    /**
     * 获取系统统计数据
     */
    @GetMapping("/statistics")
    public ApiResponse<Map<String, Object>> getSystemStatistics() {
        Map<String, Object> statistics = adminService.getSystemStatistics();
        return ApiResponse.success(statistics);
    }
    
    /**
     * 获取店铺销量统计
     */
    @GetMapping("/store-sales")
    public ApiResponse<Map<String, Object>> getStoreSalesStatistics() {
        Map<String, Object> salesData = adminService.getStoreSalesStatistics();
        return ApiResponse.success(salesData);
    }
    
    /**
     * 获取用户统计数据
     */
    @GetMapping("/user-statistics")
    public ApiResponse<Map<String, Object>> getUserStatistics() {
        Map<String, Object> userStats = adminService.getUserStatistics();
        return ApiResponse.success(userStats);
    }
    
    /**
     * 获取订单统计数据
     */
    @GetMapping("/order-statistics")
    public ApiResponse<Map<String, Object>> getOrderStatistics() {
        Map<String, Object> orderStats = adminService.getOrderStatistics();
        return ApiResponse.success(orderStats);
    }
    
    /**
     * 获取收入统计数据
     */
    @GetMapping("/revenue-statistics")
    public ApiResponse<Map<String, Object>> getRevenueStatistics() {
        Map<String, Object> revenueStats = adminService.getRevenueStatistics();
        return ApiResponse.success(revenueStats);
    }
    
    /**
     * 导出数据
     */
    @GetMapping("/export/{type}")
    public ApiResponse<String> exportData(@PathVariable String type) {
        String result = adminService.exportData(type);
        return ApiResponse.success(result);
    }
    
    /**
     * 导入数据
     */
    @PostMapping("/import/{type}")
    public ApiResponse<String> importData(@PathVariable String type, @RequestBody String data) {
        String result = adminService.importData(type, data);
        return ApiResponse.success(result);
    }
}